import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:mall/core/models/point_exchange_account.dart';
import 'package:mall/core/services/api.dart';
import 'package:provider/provider.dart';

/// 获取当前用户可以兑入的积分账户类型
class PointTypeInSheet extends StatefulWidget {
  const PointTypeInSheet({
    Key key,
    @required this.displayBalance,
  }) : super(key: key);
  final bool displayBalance;
  @override
  _PointTypeInSheetState createState() => _PointTypeInSheetState();
}

class _PointTypeInSheetState extends State<PointTypeInSheet> {
  final _list = List<PointExchangeAccount>();
  @override
  void initState() {
    super.initState();
    _pointExchangeTypeIn();
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (context, index) {
        final data = _list[index];
        return ListTile(
          title: Text("${data.name}"),
          onTap: () {
            Navigator.of(context).pop(data);
          },
        );
      },
      itemCount: _list.length,
    );
  }

  void _pointExchangeTypeIn() async {
    final api = context.read<Api>();
    try {
      final list = await api.pointExchangeTypeIn(widget.displayBalance);
      if (!mounted) {
        return;
      }
      setState(() {
        _list.addAll(list);
      });
    } on DioError catch (e) {
      EasyLoading.showToast("${e.message}");
    }
  }
}
